package com.czd.support.util;

import com.czd.support.common.Configuration;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.mail.*;

import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Author: Jerry.hu
 * Create: Jerry.hu (2014/8/26 22:16)
 * Description:java发送邮件辅助类
 */
public class MailUtils {
	private static final Log log = LogFactory.getLog(MailUtils.class);
	private static Map<String,String> mailMap = new HashMap<>();

	public static String  getLoginEmail(String mail){
		String mailSuffix = getMailSuffix(mail);
		if(mailMap.size() == 0){
			mailMap.put("163.com","mail.163.com");
			mailMap.put("126.com","mail.126.com");
			mailMap.put("qq.com","mail.qq.com");
			mailMap.put("gmail.com","gmail.com");
		}
		String loginUrl =  mailMap.get(mailSuffix);
		if(StringUtils.isBlank(loginUrl)){
			return "mail."+mailSuffix;
		}
		return loginUrl;
	}

	public static boolean sendSimpleMail(String addTo,String subject,String msg) {
		Configuration configuration = Configuration.getInstance();
		SimpleEmail email = new SimpleEmail();
		email.setCharset("UTF-8");
		//设置发送主机的服务器地址
		email.setHostName(configuration.getHostName());
		//设置收件人邮箱
		try {
		email.addTo(addTo);
		//发件人邮箱
		email.setFrom(configuration.getFrom());
		//如果要求身份验证，设置用户名、密码，分别为发件人在邮件服务器上注册的用户名和密码
		email.setAuthentication(configuration.getFrom(), configuration.getFromPwd());
		//设置邮件的主题
		email.setSubject(subject);
		//邮件正文消息
		email.setMsg(msg);
			email.send();
		}catch (EmailException e){
			e.printStackTrace();
			return  false;
		}
		return true;

	}


	/**
	 * 邮件发送方法
	 *
	 * @param mailSubject
	 *            邮件标题
	 * @param mailContent
	 *            邮件正文
	 * @throws Exception
	 */
	public static boolean sendMail(String addTo, String mailSubject, String mailContent) throws Exception {
		// 收件人列表
		try {
		log.info("系统发送邮件===============begin");
		// 创建一个eMail对象
		HtmlEmail htmlEmail = new HtmlEmail();
		htmlEmail.setDebug(true);
		htmlEmail.setCharset("UTF-8");
		// 邮件服务器地址
			htmlEmail.setHostName(Configuration.getInstance().getHostName());
		// 收件人列表
		htmlEmail.addTo(addTo);
		// 发件人
			htmlEmail.setFrom(Configuration.getInstance().getFrom());
		// 发件人登录信息
			htmlEmail.setAuthentication(Configuration.getInstance().getFrom(),Configuration.getInstance().getFromPwd());
		// 邮件标题
		htmlEmail.setSubject(mailSubject);
		// 邮件正文
		htmlEmail.setMsg(mailContent);

		// 发送邮件
			htmlEmail.send();
			log.info("系统发送邮件===============end");
			return true;
		}catch (EmailException e){
			e.printStackTrace();
			return  false;
		}

	}

	public static void sendAttachmentMail () throws EmailException {
//		创建一个Email附件

		EmailAttachment emailattachment = new EmailAttachment();

		emailattachment.setPath("/biao_05.jpg");

//     emailattachment.setURL(new URL("http://www.blogjava.net/bulktree/picture/bulktree.jpg"));

		emailattachment.setDisposition(EmailAttachment.ATTACHMENT);

		emailattachment.setDescription("This is Smile picture");

		emailattachment.setName("bulktree");

//     创建一个email

		MultiPartEmail multipartemail = new MultiPartEmail();

		multipartemail.setHostName("smtp.163.com");

		multipartemail.addTo("bulktree@126.com", "bulktree");

		multipartemail.setFrom("bulktree@163.com", "bulktree");

		multipartemail.setAuthentication("bulktree", "123456");

		multipartemail.setSubject("This is a attachment Email");

		multipartemail.setMsg("this a attachment Eamil Test");

		//添加附件

		multipartemail.attach(emailattachment);

		//发送邮件

		multipartemail.send();
		System.out.println("The attachmentEmail send sucessful!!!");
	}

	public static boolean validateEmail(String email){
		//Pattern pattern = Pattern.compile("[0-9a-zA-Z]*.[0-9a-zA-Z]*@[a-zA-Z]*.[a-zA-Z]*", Pattern.LITERAL);
		if(email == null){
			return false;
		}
		//验证开始
		//不能有连续的.
		if(email.contains("..")){
			return false;
		}
		//必须带有@
		int atCharacter = email.indexOf("@");
		if (atCharacter == -1) {
			return false;
		}

		//最后一个.必须在@之后,且不能连续出现
		if(atCharacter > email.lastIndexOf('.') || atCharacter+1 == email.lastIndexOf('.')){
			return false;
		}

		//不能以.,@结束和开始
		if (email.endsWith(".") || email.endsWith("@") || email.startsWith(".") || email.startsWith("@")) {
			return false;
		}

		return true;
	}

	private static String getMailSuffix(String email){
		return email.substring(email.lastIndexOf("@") + 1);//扩展名

	}

	public static void main(String[] args) {
		System.out.println(getMailSuffix("huwei@163.com"));
	}
}
